/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdbcdaos;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;
import jdbcpojos.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 *
 * @author piotrek
 */
@Repository
public class ProductDao {

	private JdbcTemplate jdbcTemplate;
	
	public ProductDao(){
		
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {

		this.jdbcTemplate = new JdbcTemplate(dataSource);

	}

	/**
	 * Pobieranie produktu
	 */
	public Product getProduct( int id ) throws SQLException, NamingException{

		Product product;

		product = this.jdbcTemplate.queryForObject(
					"SELECT p.id_product, p.name, p.description FROM product p WHERE p.id_product = ?",
					new Object[]{ id },
					new RowMapper<Product>() {

							public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
								Product p = new Product();
								p.setIdProduct(rs.getLong("id_product"));
								p.setName(rs.getString("name"));
								p.setDescription(rs.getString("description"));
								return p;
							}

						}

					);


		return product;

	}


}
